package com.library.staff.service;

import com.framework.Encrypt;
import com.framework.Page;
import com.library.entity.Staff;
import com.library.staff.dao.StaffDaoImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly=false)
public class StaffServiceImpl {
	
	@Resource
	private StaffDaoImpl staffDaoImpl;

    @Transactional(readOnly=true)
	public Staff login(String username, String password){
		return this.staffDaoImpl.findByUsernameAndPassword(username,Encrypt.SHA512(password));
	}

	public void addStaff(Staff staff){
		String password = Encrypt.SHA512(staff.getPassword());
		staff.setPassword(password);

		this.staffDaoImpl.saveStaff(staff);
	}

    @Transactional(readOnly=true)
	public Staff getStaff(Integer staffId){
        return this.staffDaoImpl.getStaff(staffId);
	}

    @Transactional(readOnly=true)
	public Staff getStaff(String username){
		return this.staffDaoImpl.findByUsername(username);
	}

	/*更新员工信息*/
	public void updateStaff(Staff staff){
		this.staffDaoImpl.updateStaff(staff);
	}

	/*员工修改密码*/
	public void updateStaffpassword(Staff staff){
		String newPassword = Encrypt.SHA512(staff.getPassword());
		staff.setPassword(newPassword);
		this.staffDaoImpl.updateStaff(staff);
	}

	/*更新登录时间*/
	public void updateStaffLoginTime(Staff s){
		Staff staff = this.getStaff(s.getStaffId());
		staff.setLastLoginTime(s.getLastLoginTime());

		this.staffDaoImpl.updateStaff(staff);
	}

    @Transactional(readOnly=true)
	/*查找全部员工 不分页*/
	public List<Staff> getAllStaffs(){
		return this.staffDaoImpl.findStaffsByProperty(new HashMap());
	}

	@Transactional(readOnly=true)
	/*查找全部员工 分页*/
	public Page<Staff> getAllStaffs(int pageNum,int pageSize,Map paramsMap){
		return this.staffDaoImpl.findPageByProperty(pageNum,pageSize,paramsMap);
	}

	/*根据参数查找员工列表 不分页*/
	public List<Staff> getStaffsByParams(Map paramsMap){
		return this.staffDaoImpl.findStaffsByProperty(paramsMap);
	}

	/*根据参数查找员工列表 分页*/
	public Page<Staff> getPageByParams(int pageNum,int pageSize,Map paramsMap){
		return this.staffDaoImpl.findPageByProperty(pageNum,pageSize,paramsMap);
	}


	/*删除指定员工*/
	public void deleteStaff(int staffId){
        Staff s = this.getStaff(staffId);
        this.staffDaoImpl.deleteStaff(s);
    }

	/*删除多个员工*/
	public void deleteStaffGroup(int[] staffs){
		Staff s = null;
		for(int i = 0;i < staffs.length;++i){
			s = this.getStaff(staffs[i]);
			this.staffDaoImpl.deleteStaff(s);
		}
	}

	/*等级与升级*/
	public void staffGrade(Staff staff){
		int borrowedNum = staff.getBorrowedNumber();

		if(borrowedNum < 10){
			staff.setGrade(1);
		}else if(borrowedNum < 30){
			staff.setGrade(2);
		}else if(borrowedNum < 60){
			staff.setGrade(3);
		}else if(borrowedNum < 100){
			staff.setGrade(4);
		}else{
			staff.setGrade(5);
		}
		this.updateStaff(staff);
	}

	@Transactional(readOnly=true)
	public Long getStaffsCountByParams(Map paramsMap){
		return this.staffDaoImpl.findCountByProperty(paramsMap);
	}
}
